//global for size of board
var gamesize = 4;
var done = new Array(); // array of 'flooded' elements
var notDone = new Array(); // array of not done ele's for initialJoin
// initial arrays, holding all pieces of a single color
var blueInitial = new Array();
var greenInitial = new Array();
var redInitial = new Array();
var yellowInitial = new Array();
var purpleInitial = new Array();
var orangeInitial = new Array();
function startGame()
{
	buildInits();
	done.push('#p00'); // first element in top left corner, its always 'done'
	updateDone();
/*	for (var i = 0; i < done.length; i++)*/
		colorsMatch(done[0]);// call the initial join method to group ele's touching p00
		colorsMatch(done[1]);// call the initial join method to group ele's touching p00

		colorsMatch(done[2]);// call the initial join method to group ele's touching p00
		colorsMatch(done[3]);// call the initial join method to group ele's touching p00
}
function colorsMatch (doneEle)
{
	var tempID = doneEle.replace('#p', '');
	var tempx = tempID[0];
	var tempy = tempID[1];
	var param = '';

	if (tempy != 0)
	{// check to north
alert(doneEle + " checking to north");
		param = '#p' + tempx + String(parseInt(tempy) - 1);
		if ($(param).css('background-color') == $(doneEle).css('background-color'))
		{
alert('match');
alert(param + $(param).attr('class'))
			if (!($(param).hasClass('done')))
			{
alert('pushed: ' + param);		
				done.push(param);
				updateDone();
alert(param + $(param).attr('class'))
			}
alert(done);
		}
	}
	if (tempx != gamesize-1)
	{// check to east
alert(doneEle + " checking to east");
		param = '#p' + String(parseInt(tempx) + 1) + tempy;
		if ($(param).css('background-color') == $(doneEle).css('background-color'))
		{// element in done array has the same color as its eastern neighbor
alert('match');
alert(param + $(param).attr('class'))
			if (!($(param).hasClass('done')))
			{// 'param' element is not in done array already
alert('pushed: ' + param);
				done.push(param);
				updateDone();
alert(param + $(param).attr('class'))
			}
alert(done);
		}
	}
	if (tempy != gamesize-1)
	{// check to south 
alert(doneEle + " checking to south");
		param = '#p' + tempx + String(parseInt(tempy) + 1);
		if ($(param).css('background-color') == $(doneEle).css('background-color'))
		{
alert('match');
alert(param + $(param).attr('class'))
			if (!($(param).hasClass('done')))
			{
alert('pushed: ' + param);
				done.push(param);
				updateDone();
alert(param + $(param).attr('class'))
			}
alert(done);
		}
	}
	if (tempx != 0)
	{// check to west
alert(doneEle + " checking to west");
		param = '#p' + String(parseInt(tempx) - 1) + tempy;
		if ($(param).css('background-color') == $(doneEle).css('background-color'))
		{
alert('match');
alert(param + $(param).attr('class'))
			if (!($(param).hasClass('done')))
			{
alert('pushed: ' + param);
				done.push(param);
				updateDone();
alert(param + $(param).attr('class'))
			}
alert(done);
		}
	}
alert('final ' + done);
	return;
}
function move(inputColor)
{
	updateDone();
	var tempArr = eval(whichArray(inputColor)); // scary setting tempArr to the right initial
	var temp = '';
	for (var i = 0; i < done.length; i++)
	{// remove all classes from done elements, add input color
		var param = 'pieces done ' + inputColor;
		//alert(param);
		if ($(done[i]).hasClass('done'))
		{// element is in done and now has new color and 'pieces' 'done' and 'color'
			$(done[i]).removeClass();
			$(done[i]).addClass(param);
		}
	}
	for (var j = 0; j < tempArr.length; j++)
	{// tempArr[j] is like 'p00'
		for (var s = 0; s < done.length; s++)
		{// done[s] is like '#p00'
			if (touches(done[s], tempArr[j]))
			{// element in initial color array touches element in done, join them
				if (!$(tempArr[j]).hasClass('done'))
				{// if the tempArr[j] is not in done already, add it.
					done.push(tempArr[j]);
				}
			}
		}
	}
	updateDone();
}
//use the same colors match algorithm to avoid the missed boxes that keep happening
//
//like this:
//	change done to right color
//	for(all in done)
//		colors match(doneele)
//			check north, south, east, west
//			compare css colors, if colors match, & not in done already (hasClass)
//			push that param ele onto done
//			check next dir's
function touches(doneEle, initialEle)
{// doneEle = '#p00', initialEle = 'p00'
	var tempInit = initialEle.replace('#p', '');
	var tempx = tempInit[0];
	var tempy = tempInit[1];
	var param = '';
	if (tempy != 0)
	{// not on top row, check to north
		param = 'p' + tempx + String(parseInt(tempy) - 1);
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	if (tempx != gamesize-1)
	{// not on far right, check to east
		param = 'p' + String(parseInt(tempx) + 1) + tempy;
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	if (tempy != gamesize-1)
	{// not on bottom row, check to south
		param = 'p' + tempx + String(parseInt(tempy) + 1);
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	if (tempx != 0)
	{// not on far left, check to west
		param = 'p' + String(parseInt(tempx) - 1) + tempy;
		if (doneEle.search(param) != -1)
		{
			return true;
		}
	}
	return false;
}
function buildInits()
{// hardcode initial arrays for test purposes
	redInitial.push('#p00');
	redInitial.push('#p10');
	redInitial.push('#p01');
	redInitial.push('#p02');
	redInitial.push('#p33');
	yellowInitial.push('#p20');
	yellowInitial.push('#p21');
	yellowInitial.push('#p31');
	orangeInitial.push('#p30');
	orangeInitial.push('#p11');
	blueInitial.push('#p12');
	blueInitial.push('#p22');
	blueInitial.push('#p32');
	blueInitial.push('#p13');
	blueInitial.push('#p23');
	greenInitial.push('#p03');
}
function whichArray(inputColor)
{// returns array name to use in move function
	switch(inputColor)
	{
		case 'blue':
			return 'blueInitial';
			break;
		case 'red':
			return 'redInitial';
			break;
		case 'orange':
			return 'orangeInitial';
			break;
		case 'yellow':
			return 'yellowInitial';
			break;
		case 'purple':
			return 'purpleInitial';
			break;
		case 'green':
			return 'greenInitial';
			break;
		
		default:
			return true;
	}
}
function updateDone()
{
	for (var j = 0; j < done.length; j++)
	{// each element in done array
		if (!($(done[j]).hasClass('done')))
		{// if element doesnt have 'done' class, add it.
			$(done[j]).addClass('done');
		}
	}

}
/*********				old code for full game
function generateBoard()
{
	for (var i = 0; i < gamesize; i++)
	{//ycoord
		for (var j = 0; j < gamesize; j++)
		{//xcoord
			makediv(i,j);	
		}
	}
}
function makediv(i,j)
{
	var board = document.getElementById("gameboard");
	var newdiv = document.createElement("div");
	var theID = '#p' + j + i; // id to use in arrays for comparison against doneEle's
	newdiv.id = 'p' + j + i;
	var random = Math.floor(Math.random()*6);
	switch(random)
	{
		// build initial color arrays to be used, contain all elements of a color
		// give class names
		case 0:
			newdiv.className = 'pieces green';
			greenInitial.push(theID);
			break;
		case 1:
			newdiv.className = 'pieces purple';
			purpleInitial.push(theID);
			break;
		case 2:
			newdiv.className = 'pieces yellow';
			yellowInitial.push(theID);
			break;
		case 3:
			newdiv.className = 'pieces red';
			redInitial.push(theID);
			break;
		case 4:
			newdiv.className = 'pieces orange';
			orangeInitial.push(theID);
			break;
		case 5:
			newdiv.className = 'pieces blue';
			blueInitial.push(theID);
			break;
		default:
			return;
	}
	notDone.push(theID);
	board.appendChild(newdiv);
}
/***********************************************
*********************end build board************ 
***********************************************/

/*check to see if initial first element touches colors already!
//push element p00 onto an array of joined pieces done* (add 'done' to class)
//when creating pieces, make a list of colors as theyre built to use later
//wait for input
//	get colorInput array for the color input (all have class *color* in common)
//	change done*'s color class name to new input color
//	check all elements in colorInput array
//		see if they touch elements in done*
		if they do, add them to done* 
		remove elements you added to done* from initial added color array
		put those elements into a  temp array,  
		call a function with that temp and the newly updated initial color array,
			if temp elements touch initial color array elements
			repeat
				add the initial color arr elements that touch to done*
				remove them from initial color
				delete temp, set it equal to ones from above
				call again on temp and initial color arr
			until no more matches 
			this may not be needed if done.length updates, and checks every single element that was
		added too???
		if done.length == #ofpcs, they win
		else
			if #ofMoves !< #allowed, game over
			else play on.*/

